package com.ctvit.breadking.news.core.solr.utils;

import com.ctvit.breadking.news.core.constant.CommonConstant;
import com.ctvit.breadking.news.core.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.common.SolrDocument;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;

/**
 * SolrQuery工具类
 *
 * @author heyingcheng
 * @date 2024/10/2
 */
public class SolrQueryUtils {

    private SolrQueryUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static final String SOLR_QUERY_ALL = "*:*";

    public static String obtainStringValue(SolrDocument queryResult, String key) {
        return obtainStringValue(queryResult, key, StringUtils.EMPTY);
    }

    public static String obtainStringValue(SolrDocument queryResult, String key, String defaultValue) {
        return ObjectUtils.defaultIfNull((String) queryResult.get(key), defaultValue);
    }

    public static Integer obtainIntegerValue(SolrDocument queryResult, String key) {
        return obtainIntegerValue(queryResult, key, 0);
    }

    public static Integer obtainIntegerValue(SolrDocument queryResult, String key, Integer defaultValue) {
        return ObjectUtils.defaultIfNull((Integer) queryResult.get(key), defaultValue);
    }

    public static Long obtainLongValue(SolrDocument queryResult, String key) {
        return obtainLongValue(queryResult, key, 0L);
    }

    public static Long obtainLongValue(SolrDocument queryResult, String key, Long defaultValue) {
        return ObjectUtils.defaultIfNull((Long) queryResult.get(key), defaultValue);
    }

    public static Date obtainDateValue(SolrDocument queryResult, String key) {
        return (Date) queryResult.get(key);
    }

    public static String obtainDateString(SolrDocument queryResult, String key) {
        Date date = obtainDateValue(queryResult, key);
        if (Objects.nonNull(date)) {
            return DateUtils.formatYmdHms(date);
        }
        return StringUtils.EMPTY;
    }

    public static String obtainUtcString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        // 设置时区为UTC
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        return sdf.format(date);
    }

    public static void likeString(List<String> conditionList, String string, String field) {
        if (StringUtils.isNotEmpty(string)) {
            String textCondition = StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, CommonConstant.ASTERISK, string, CommonConstant.ASTERISK, CommonConstant.RIGHT_PARENTHESES);

            String[] keywords = string.split(StringUtils.SPACE);
            if (keywords.length > 1) {
                Set<String> conditionSet = new LinkedHashSet<>();
                for (String keyword : keywords) {
                    if (StringUtils.isNotBlank(keyword)) {
                        conditionSet.add(StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, CommonConstant.ASTERISK, keyword, CommonConstant.ASTERISK, CommonConstant.RIGHT_PARENTHESES));
                    }
                }

                if (CollectionUtils.isNotEmpty(conditionSet)) {
                    String splitCondition = StringUtils.joinWith(CommonConstant.LOGIC_AND, conditionSet.toArray(new Object[0]));
                    textCondition = StringUtils.join(CommonConstant.LEFT_PARENTHESES, textCondition, CommonConstant.LOGIC_OR, CommonConstant.LEFT_PARENTHESES, splitCondition, CommonConstant.RIGHT_PARENTHESES, CommonConstant.RIGHT_PARENTHESES);
                }
            }

            conditionList.add(textCondition);
        }
    }

    public static void likeText(List<String> conditionList, String text, String field) {
        if (StringUtils.isNotEmpty(text)) {
            String textCondition = StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, CommonConstant.DOUBLE_QUOTATION_MARK, text, CommonConstant.DOUBLE_QUOTATION_MARK, CommonConstant.RIGHT_PARENTHESES);

            String[] keywords = text.split(StringUtils.SPACE);
            if (keywords.length > 1) {
                Set<String> conditionSet = new LinkedHashSet<>();
                for (String keyword : keywords) {
                    if (StringUtils.isNotBlank(keyword)) {
                        conditionSet.add(StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, CommonConstant.DOUBLE_QUOTATION_MARK, keyword, CommonConstant.DOUBLE_QUOTATION_MARK, CommonConstant.RIGHT_PARENTHESES));
                    }
                }

                if (CollectionUtils.isNotEmpty(conditionSet)) {
                    String splitCondition = StringUtils.joinWith(CommonConstant.LOGIC_AND, conditionSet.toArray(new Object[0]));
                    textCondition = StringUtils.join(CommonConstant.LEFT_PARENTHESES, textCondition, CommonConstant.LOGIC_OR, CommonConstant.LEFT_PARENTHESES, splitCondition, CommonConstant.RIGHT_PARENTHESES, CommonConstant.RIGHT_PARENTHESES);
                }
            }

            conditionList.add(textCondition);
        }
    }

    public static void eq(List<String> conditionList, String text, String field) {
        if (StringUtils.isNotEmpty(text)) {
            conditionList.add(StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, text, CommonConstant.RIGHT_PARENTHESES));
        }
    }

    public static void eq(List<String> conditionList, Integer number, String field) {
        if (Objects.nonNull(number)) {
            conditionList.add(StringUtils.join(CommonConstant.LEFT_PARENTHESES, field, CommonConstant.COLON, number, CommonConstant.RIGHT_PARENTHESES));
        }
    }

}
